package shop_cart

import (
	"context"
	"github.com/gin-gonic/gin"
	"mxshop_api/order-web/forms"
	"mxshop_api/order-web/global"
	"mxshop_api/order-web/proto"
	"mxshop_api/user-web/api"
	"net/http"
)

func List(ctx *gin.Context) {

	//参数验证
	var shopCartItemForm forms.ShopCartItemForm
	if err := ctx.ShouldBind(&shopCartItemForm); err != nil {
		api.HandleValidatorError(ctx, err)
		return
	}
	//获取购物车列表
	cartItemList, err := global.OrderSrvConn.CartItemList(context.Background(), &proto.UserInfo{Id: shopCartItemForm.GoodsId})
	if err != nil {
		api.HandlerGrpcToHttp(err, ctx)
		return
	}
	var goodsIds []int32
	for _, cartItem := range cartItemList.Data {
		goodsIds = append(goodsIds, cartItem.GoodsId)
	}
	//获取商品列表返回信息
	goodsList, err := global.GoodsSrvConn.BatchGetGoods(context.Background(), &proto.BatchGoodsIdInfo{Id: goodsIds})
	if err != nil {
		api.HandlerGrpcToHttp(err, ctx)
		return
	}

	var responseMap []map[string]interface{}
	for _, cartItem := range cartItemList.Data {
		for _, goods := range goodsList.Data {
			if cartItem.GoodsId == goods.Id {
				responseMap = append(responseMap, map[string]interface{}{
					"id":         cartItem.Id,
					"goods_id":   cartItem.GoodsId,
					"good_name":  goods.Name,
					"good_image": goods.GoodsFrontImage,
					"good_price": goods.ShopPrice,
					"nums":       cartItem.Nums,
					"checked":    cartItem.Checked,
				})
			}
		}
	}

	ctx.JSON(http.StatusOK, gin.H{
		"total": cartItemList.Total,
		"data":  responseMap,
	})

}

func New(ctx *gin.Context) {

	//参数验证
	//获取商品信息
	//获取库存信息比较库存
	//创建购物车数据
}

func Update(ctx *gin.Context) {
	//获取参数
	//转换参数类型
	//调用方法
	//返回结果
}

func Delete(ctx *gin.Context) {

	//获取参数
	//转换参数类型
	//调用方法
	//返回结果
}
